package com.hippo.provider.controller.export;

import com.hippo.provider.config.Export;
import com.hippo.provider.enums.ExportEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @version 1.0.0
 * @auther tangxl
 * @Date 2023/6/7/007 21:47
 */
@Tag(name = "导入、导出测试接口", description = "导入、导出测试接口")
@RestController
@RequestMapping("/export")
public class ExportTestController {

    @Operation(summary = "单文件上传(拦截)")
    @PostMapping(value = "/exportOne")
    @Export(value = ExportEnum.IMPORT)
    public void exportOne(@RequestBody MultipartFile file) {
        String fileName = file.getOriginalFilename();
        try(
                OutputStream outputStream = new FileOutputStream("D:\\Tools\\workspace\\learning\\java-study\\study-services\\service-provider\\src\\main\\resources\\importFile\\"+fileName);
        ) {
            // 写入文件
            byte[] bytes = new byte[1024];
            int len = 0;
            InputStream inputStream = file.getInputStream();
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("单文件上传:"+fileName);
    }

    @Operation(summary = "单文件上传(不拦截)")
    @PostMapping(value = "/exportOne2")
    public void exportOne2(@RequestBody MultipartFile file) {
        String fileName = file.getOriginalFilename();
        try(
                OutputStream outputStream = new FileOutputStream("D:\\Tools\\workspace\\learning\\java-study\\study-services\\service-provider\\src\\main\\resources\\importFile\\"+fileName);
        ) {
            // 写入文件
            byte[] bytes = new byte[1024];
            int len = 0;
            InputStream inputStream = file.getInputStream();
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("单文件上传:"+fileName);
    }
    @Operation(summary = "单文件下载(拦截)")
    @GetMapping(value = "/exportThree")
    @Export(value = ExportEnum.EXPORT)
    public void exportThree(HttpServletResponse response) {
        String fileName = "4306049cd05348bfa76b969b81816a42.docx";

        try(
                InputStream inputStream = new FileInputStream("D:\\Tools\\SrcProject\\learning\\java-study\\study-services\\service-provider\\src\\main\\resources\\file\\"+fileName);
                OutputStream outputStream = response.getOutputStream();
        ) {
            // 设置响应头
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"utf-8"));//通知客户端已下载的方式接受数据
            response.setHeader("Content-Type", "application/octet-stream");//告诉浏览器输出内容为流
            response.setContentType("application/octet-stream; charset=UTF-8");
            // 写入文件
            //流数据交换，每次交换10k数据大小 （1024k = 1m）
            byte[] bytes = new byte[1024*10];
            int read;
            do {
                read = inputStream.read(bytes);
                outputStream.write(bytes,0,read);
            }while (-1 != read);

        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("单文件下载:"+fileName);
    }
}
